# -*- encoding=utf8 -*-
__author__ = "yupf"

from airtest.core.api import *
# import mac_say

from poco.drivers.ios import iosPoco


# wait(Template(r"tpl1685695441918.png", record_pos=(-0.368, -0.671), resolution=(1242, 2208)))

# # add(poco,'2229290003014790','test01s','07/24','112')
# touch(Template(r"tpl1685695513447.png", record_pos=(0.419, -0.673), resolution=(1242, 2208)))

def new_card(poco: iosPoco, card_info, mac_say):
    keyevent("HOME")
    keyevent("HOME")  # 多按一步home,避免从应用里返回桌面时，停留在其他页面。
    sleep(3)
    while True:
        b = exists(Template(r"tpl1685979307694.png", record_pos=(-0.107, 0.235), resolution=(1242, 2208)))
        if b:
            poco("钱包").click()
            break
        else:
            swipe((0.5, 0.5), (0.1, 0.5))

    # start_app("com.apple.PassbookUIService")
    touch(Template(r"tpl1685695513447.png", record_pos=(0.419, -0.673), resolution=(1242, 2208)))
    assert_exists(Template(r"tpl1685695831130.png", record_pos=(-0.007, -0.568), resolution=(1242, 2208)),
                  "判断是否进入添加卡片页")
    poco("借记卡或信用卡", type='StaticText').click()
    wait(Template(r"tpl1685696160384.png", record_pos=(-0.295, -0.201), resolution=(1242, 2208)))

    swipe((0.5, 0.5), vector=(0.5, 0.3))

    poco("添加其他卡片", type='StaticText').click()

    wait(Template(r"tpl1685698357537.png", record_pos=(-0.002, -0.026), resolution=(1242, 2208)))
    sleep(15)
    poco('扫描或添加卡', type="Button").click()

    # 手动输入卡片详情
    poco('手动输入卡片详细信息', type="Button").click()
    wait(Template(r"tpl1685698745769.png", record_pos=(-0.001, -0.6), resolution=(1242, 2208)))

    poco("卡号", type="TextField").click()
    text(card_info['card_num'])
    touch(Template(r"tpl1685699218683.png", record_pos=(0.388, -0.728), resolution=(1242, 2208)))

    wait(Template(r"tpl1685845159955.png", record_pos=(-0.002, -0.595), resolution=(1242, 2208)))
    poco('姓名', type='TextField').click()
    text(card_info['payer'])
    # os.system("say --voice='Mei-Jia' 您好，我是美佳，您的贴心小助手，请输入银行卡有效期，输入完后，记得输入yes,让脚本继续!")
    mac_say.say(f'您好，请输入银行卡有效期:{card_info["card_expire"]}，输入完后，命令行输入yes or y,让脚本继续!')

    while True:
        msg = input(f'输入银行卡有效期:{card_info["card_expire"]},完毕后输入yes or y，然后继续\n')
        if msg in ['yes', 'y']:
            break

    # text('04/23')
    poco('安全码', type='TextField').click()
    text(card_info['cvv'])
    touch(Template(r"tpl1685699218683.png", record_pos=(0.388, -0.728), resolution=(1242, 2208)))
    wait(Template(r"tpl1685847477044.png", record_pos=(-0.009, -0.722), resolution=(1242, 2208)), 30)
    touch(Template(r"tpl1685847485416.png", record_pos=(0.411, 0.83), resolution=(1242, 2208)))
